package isae.c2.android.depense.DB;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DepenseDBHelper extends SQLiteOpenHelper {
    /** * le nom de la base de donnees*/
    private static final String DATABASE_NAME = "Transaction_db";
    
    /** * version de la base de donnees*/
    private static final int DATABASE_VERSION = 1;

    /** * ecrire la requette qui cree la table "category"*/
    private static final String TABLE_CAT_CREATE = "create table category (_id integer primary key autoincrement, "
            + "cat_type text not null, code text not null, description text not null);";
    
    /** * ecrire la requette qui cree la table "tranactions"*/
    private static final String TABLE_TRANX_CREATE = "create table transactions (_id integer primary key autoincrement, "
        + "description text not null, vdate date not null, amount float not null, "
            +"cat_id integer not null, FOREIGN KEY(cat_id) REFERENCES category(_id));";
    
    /** * creer la base de donnees*/
    public DepenseDBHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    /** * pendant la creation de la base de donnees, la methode ci-dessous est appelee, qui
         * nous permet de creer les tables*/
    @Override
    public void onCreate(SQLiteDatabase database) {
        database.execSQL(TABLE_CAT_CREATE);
        database.execSQL(TABLE_TRANX_CREATE);
    }

    /** * Cette methode est appelee quand la base de donnees subit un changement
         * exemple l'incrementation de la version de la base de donnees*/
    @Override
    public void onUpgrade(SQLiteDatabase database, int oldVersion,
            int newVersion) {
        /** * changer la version de la base de donnees*/
        Log.w(DepenseDBHelper.class.getName(),
                "Upgrading database from version " + oldVersion + " to "
                        + newVersion + ", which will destroy all old data");
        /** * supprimer la table categorie*/
        database.execSQL("DROP TABLE IF EXISTS category");
        /** * supprimer la table categorie*/
        database.execSQL("DROP TABLE IF EXISTS transactions");
        /** * re-creer les tables*/
        onCreate(database);
    }
}